#include "camera_thread.h"
#include "mt_timer.h"
#include "uthash.h"


int photo = 0;
char is_show = 1;
char is_cameraopen = 0;
int full = 1;
int displaywidth  = DISPLAY_WIDTH;
int displayheight = SCREEN_HEIGHT;
static int frame = 0;
int startPlayAvi = 0;
int pausePlayAvi = 0;
char videoPath[50];
char h265Path[50];



extern int awb_rgain;
extern int awb_grgain;
extern int awb_gbgain;
extern int awb_bgain;
extern int csc_constrast;
extern int ae_mode;

extern int DE_enable;
extern int DE_model;
extern int DE_GlobaGain;
extern int DE_GainLF;
extern int DE_GainHF;
extern int LDCI_en;
extern int LDCI_mode;
extern int LDCI_LPFSigma;
extern int LDCI_Blc;
extern int LDCI_posmean;
extern int LDCI_possigma;
extern int LDCI_poswgt;
extern int LDCI_negmean;
extern int LDCI_negsigma;
extern int LDCI_negwgt;

//===================================================================================================================================
// DVP isp ctrl
//===================================================================================================================================
void mppisp_ov426_ctrl()
{
    //PutIniKeyInt(char *title, char *key, int val, char *filename)
    PutIniKeyInt("Option","connect_device",   1,  PARAM_FILE);                      //1=OV6946,2=OS01A
    PutIniKeyInt("Option","Rotate",           0,  PARAM_FILE);                      //0 = 旋转0度.
    PutIniKeyInt("Option","Mirror",           0,  PARAM_FILE);                      //0 = 上下镜像.

//  PutIniKeyInt("isp","awb_rgain",           240,  PARAM_FILE);                    //R  gain.       2023-04-29 取消
//  PutIniKeyInt("isp","awb_gbgain",          260,  PARAM_FILE);                    //Gb gain.       2023-04-29 取消
//  PutIniKeyInt("isp","awb_grgain",          260,  PARAM_FILE);                    //Gr gain.       2023-04-29 取消
//  PutIniKeyInt("isp","awb_bgain",           350,  PARAM_FILE);                    //B  gain.       2023-04-29 取消
//  PutIniKeyInt("isp","ae_mode",             2,    PARAM_FILE);                    //2 =bypass.     2023-04-29 取消
//  PutIniKeyInt("isp","csc_constrast",       45,   PARAM_FILE);                    //csc_constrast. 2023-04-29 取消

    awb_rgain       = GetIniKeyInt("isp","sdawb_rgain",PARAM_FILE);                 //白平衡rgain
    awb_grgain      = GetIniKeyInt("isp","sdawb_grgain",PARAM_FILE);                //白平衡grgain
    awb_gbgain      = GetIniKeyInt("isp","sdawb_gbgain",PARAM_FILE);                //白平衡gbgain
    awb_bgain       = GetIniKeyInt("isp","sdawb_bgain",PARAM_FILE);                 //白平衡bgain
    ae_mode         = GetIniKeyInt("isp","ae_mold",PARAM_FILE);                     //曝光模式设置：ae_mode =0 OP_TYPE_AUTO;ae_mode=1 OP_TYPE_MANUAL;ae_mode=2 bByPass;
    csc_constrast   = GetIniKeyInt("isp","csc_constrast",PARAM_FILE);               //对比度

    HI_U16 tmp_ccmmatrix[9];
    tmp_ccmmatrix[0] = 0x01F1; // settings.value("isp/ccm_rr").toInt();             //465
    tmp_ccmmatrix[1] = 0x80E1; // settings.value("isp/ccm_rg").toInt();             //32984
    tmp_ccmmatrix[2] = 0x8010; // settings.value("isp/ccm_rb").toInt();             //32775
    tmp_ccmmatrix[3] = 0x8076; // settings.value("isp/ccm_gr").toInt();             //32832
    tmp_ccmmatrix[4] = 0x01B8; // settings.value("isp/ccm_gg").toInt();             //449
    tmp_ccmmatrix[5] = 0x8042; // settings.value("isp/ccm_gb").toInt();             //32897
    tmp_ccmmatrix[6] = 0x803D; // settings.value("isp/ccm_br").toInt();             //1
    tmp_ccmmatrix[7] = 0x8146; // settings.value("isp/ccm_bg").toInt();             //32959
    tmp_ccmmatrix[8] = 0x0283; // settings.value("isp/ccm_bb").toInt();             //446

    //AE
    //mppIspAEDisable();
    mppIsp_AUTO_EXPOSURE(0, 2 );                                                    //2=bypass

    //AWB
    //mppIspAwb(0, 2, 5500, 240, 260, 260, 350);
    mppIspAwb(0, 2, 5500, awb_rgain, awb_gbgain, awb_grgain, awb_bgain);

    //SHARPEN
    //mppIspSharpen(0, 1, 1000, 2000, 15, 20, 225, 16, 225, 16, 100, 700, 700, 200, 12, 200, 200, 50, 20, 20, 20, 20, 200);
    mppIspSharpen(0, 2, 128,   96, 100, 127, 225, 16, 115, 16, 127, 200, 400, 8, 9, 128, 160, 53, 28, 32, 28, 23, 160);

    //Gamma 
    mppIspGammaCtrl(0, 0, 1);

    //CCM
    mppIspCcmCtrl(0, 1, tmp_ccmmatrix);

    //降噪设置  Bayer NR
    ISP_NR_ATTR_S stNRAttr;
    HI_MPI_ISP_GetNRAttr(0, &stNRAttr);
    stNRAttr.bEnable        = HI_TRUE;
    stNRAttr.enOpType       = OP_TYPE_MANUAL;           //手动降噪模式
    stNRAttr.bNrLscEnable   = HI_FALSE;                 //静止参考 LSC 增益功能
    stNRAttr.stManual.u16CoringWgt = 0x240;             //RW;Range:[0x0,0xc80];随机噪声保留强度，值越大，保留的随机噪声越多;
    stNRAttr.stManual.u8FineStr    = 0x80;              //RW;Range:[0x0,0x80];亮噪去除强度，值越大，亮噪去除强度越大;
    HI_MPI_ISP_SetNRAttr(0,&stNRAttr);

    //镜头阴影校正   需要标定参数 LSC
    //ISP_SHADING_ATTR_S stShadingAttr;
    //HI_MPI_ISP_GetMeshShadingAttr(0, &stShadingAttr);
    //stShadingAttr.u16MeshStr = 0;
    //stShadingAttr.u16BlendRatio = 50;
    //HI_MPI_ISP_SetMeshShadingAttr(0, &stShadingAttr);

    //动态坏点校正
    ISP_DP_DYNAMIC_ATTR_S stDPDynamicAttr;
    HI_MPI_ISP_GetDPDynamicAttr(0,&stDPDynamicAttr);
    stDPDynamicAttr.enOpType = OP_TYPE_MANUAL;
    int tmp_lschr0          = GetIniKeyInt("isp","lsc_hr0",PARAM_FILE);
    int tmp_lschr1          = GetIniKeyInt("isp","lsc_hr1",PARAM_FILE);
    stDPDynamicAttr.stManual.u16BlendRatio = tmp_lschr0;
    stDPDynamicAttr.stManual.u16Strength = tmp_lschr1;
    HI_MPI_ISP_SetDPDynamicAttr(0,&stDPDynamicAttr); 

    //========================================================================================================
    //去 FPN 模块就是要把这些固定模式噪声消除,需要标定黑帧，但是并没有标定,（Fixed Pattern Noise）
    ISP_FPN_ATTR_S pstFPNAttr;
    HI_MPI_ISP_GetFPNAttr(0, &pstFPNAttr);
    pstFPNAttr.bEnable=HI_TRUE;
    pstFPNAttr.enOpType=OP_TYPE_AUTO;
    HI_MPI_ISP_SetFPNAttr(0, &pstFPNAttr);

    //========================================================================================================
    //DE 改善噪声形态， 使得噪声均匀细碎， 同时改善边缘毛糙。
    //========================================================================================================
    mppIspDeCtrl(0,DE_enable,DE_model,DE_GlobaGain,DE_GainLF,DE_GainHF) ;

    //========================================================================================================
    //LDCI 是局域自动对比度增强的简称（Local Dynamic Contrast Improvement）
    //========================================================================================================
    mppIspLdciCtrl(0,LDCI_en,LDCI_mode,LDCI_LPFSigma, LDCI_Blc, LDCI_posmean,LDCI_possigma,LDCI_poswgt, LDCI_negmean,LDCI_negsigma,LDCI_negwgt);
    system("sh /usr/qt5.12/demos/sensor_rst.sh > /dev/null");

}

//===================================================================================================================================
// MIPI isp ctrl
//===================================================================================================================================
void mppisp_os01a_ctrl()
{
    //PutIniKeyInt(char *title, char *key, int val, char *filename)
    PutIniKeyInt("Option","connect_device",   2,    PARAM_FILE);                    //1=OV6946,2=OS01A
    PutIniKeyInt("Option","Rotate",           3,    PARAM_FILE);                    //3 = 旋转270度.
    PutIniKeyInt("Option","Mirror",           0,    PARAM_FILE);                    //0 = 上下镜像

//  PutIniKeyInt("isp","awb_rgain",           245,  PARAM_FILE);                    //R  gain.
//  PutIniKeyInt("isp","awb_gbgain",          260,  PARAM_FILE);                    //Gb gain.
//  PutIniKeyInt("isp","awb_grgain",          260,  PARAM_FILE);                    //Gr gain.
//  PutIniKeyInt("isp","awb_bgain",           243,  PARAM_FILE);                    //B  gain.
//  PutIniKeyInt("isp","ae_mode",             0,    PARAM_FILE);                    //0 =自动曝光.
//  PutIniKeyInt("isp","csc_constrast",       60,   PARAM_FILE);                    //2 =bypass.

    awb_rgain       = GetIniKeyInt("isp","hdawb_rgain",PARAM_FILE);                 //白平衡rgain
    awb_grgain      = GetIniKeyInt("isp","hdawb_grgain",PARAM_FILE);                //白平衡grgain
    awb_gbgain      = GetIniKeyInt("isp","hdawb_gbgain",PARAM_FILE);                //白平衡gbgain
    awb_bgain       = GetIniKeyInt("isp","hdawb_bgain",PARAM_FILE);                 //白平衡bgain
    ae_mode         = GetIniKeyInt("isp","ae_mold",PARAM_FILE);                     //曝光模式设置：ae_mode =0 OP_TYPE_AUTO;ae_mode=1 OP_TYPE_MANUAL;ae_mode=2 bByPass;
    csc_constrast   = GetIniKeyInt("isp","csc_constrast",PARAM_FILE);               //对比度

    HI_U16 tmp_ccmmatrix[9];
    tmp_ccmmatrix[0] = 0x01F1; // settings.value("isp/ccm_rr").toInt();             //465
    tmp_ccmmatrix[1] = 0x80E1; // settings.value("isp/ccm_rg").toInt();             //32984
    tmp_ccmmatrix[2] = 0x8010; // settings.value("isp/ccm_rb").toInt();             //32775
    tmp_ccmmatrix[3] = 0x8076; // settings.value("isp/ccm_gr").toInt();             //32832
    tmp_ccmmatrix[4] = 0x01B8; // settings.value("isp/ccm_gg").toInt();             //449
    tmp_ccmmatrix[5] = 0x8042; // settings.value("isp/ccm_gb").toInt();             //32897
    tmp_ccmmatrix[6] = 0x803D; // settings.value("isp/ccm_br").toInt();             //1
    tmp_ccmmatrix[7] = 0x8146; // settings.value("isp/ccm_bg").toInt();             //32959
    tmp_ccmmatrix[8] = 0x0283; // settings.value("isp/ccm_bb").toInt();             //446

    //=======================================================================================================
    mppIsp_AUTO_EXPOSURE(0, 0);                                                     //Auto AE

    //mppIspAwb(0, 2, 5500, 245, 260, 260, 243);
    mppIspAwb(0, 2, 5500, awb_rgain, awb_gbgain, awb_grgain, awb_bgain);

    /*
    void mppIspSharpen(1 ViPipe,2 mode,3 TextureFreq,4 EdgeFreq,5 Overshoot,6 UnderShoot,
            7 umax,8 umin,9 vmax,10 vmin,11 luma,12 texturestr,13 edgestr,14 supstr,
            15 supadj,16 detailctrl,17 detailctrlthr,18 edgefilt,19 rgain,20 ggain,21 bgain,
            22 skingain,23 maxsharp);
    mppIspSharpen(0,2,128,96,100,127,umax,umin ,vmax,vmin, 127,300,400,8,9,128,160,53,28,32,28,23,160)//官方推荐值
    */
      mppIspSharpen(0, 2, 128,   96, 100, 127, 225, 16, 115, 16, 127, 200, 400, 8, 9, 128, 160, 53, 28, 32, 28, 23, 160); 
    //mppIspSharpen(0, 2, 128,   96, 100, 127, 225, 16, 115, 16, 127, 300, 400, 8, 9, 128, 160, 53, 28, 32, 28, 23, 160); //官方推荐值
    //mppIspSharpen(0, 2, 1000, 2000, 40,  50, 225, 16, 115, 16, 127,  96, 100, 8, 9, 128, 160, 53, 28, 32, 28, 23, 160);

    //Gamma
    mppIspGammaCtrl(0, 0, 1);

    //CCM
    mppIspCcmCtrl(0, 1, tmp_ccmmatrix);

    //降噪设置  Bayer NR
    ISP_NR_ATTR_S stNRAttr;
    HI_MPI_ISP_GetNRAttr(0, &stNRAttr);
    stNRAttr.bEnable        = HI_TRUE;
    stNRAttr.enOpType       = OP_TYPE_MANUAL;           //手动降噪模式
    stNRAttr.bNrLscEnable   = HI_FALSE;                 //静止参考 LSC 增益功能
    stNRAttr.stManual.u16CoringWgt = 0x240;             //RW;Range:[0x0,0xc80];随机噪声保留强度，值越大，保留的随机噪声越多;
    stNRAttr.stManual.u8FineStr    = 0x80;              //RW;Range:[0x0,0x80];亮噪去除强度，值越大，亮噪去除强度越大;
    HI_MPI_ISP_SetNRAttr(0,&stNRAttr);

    //镜头阴影校正   需要标定参数 LSC-- 关闭
    //ISP_SHADING_ATTR_S stShadingAttr;
    //HI_MPI_ISP_GetMeshShadingAttr(0, &stShadingAttr);
    //stShadingAttr.u16MeshStr = 0;
    //stShadingAttr.u16BlendRatio = 50;
    //HI_MPI_ISP_SetMeshShadingAttr(0, &stShadingAttr);

    //动态坏点校正
    ISP_DP_DYNAMIC_ATTR_S stDPDynamicAttr;
    HI_MPI_ISP_GetDPDynamicAttr(0,&stDPDynamicAttr);
    stDPDynamicAttr.enOpType = OP_TYPE_MANUAL;
    int tmp_lschr0          = GetIniKeyInt("isp","lsc_hr0",PARAM_FILE);
    int tmp_lschr1          = GetIniKeyInt("isp","lsc_hr1",PARAM_FILE);
    stDPDynamicAttr.stManual.u16BlendRatio = tmp_lschr0;
    stDPDynamicAttr.stManual.u16Strength = tmp_lschr1;
    HI_MPI_ISP_SetDPDynamicAttr(0,&stDPDynamicAttr); 

    //========================================================================================================
    //去 FPN 模块就是要把这些固定模式噪声消除,需要标定黑帧，但是并没有标定,（Fixed Pattern Noise）
    ISP_FPN_ATTR_S pstFPNAttr;
    HI_MPI_ISP_GetFPNAttr(0, &pstFPNAttr);
    pstFPNAttr.bEnable=HI_TRUE;
    pstFPNAttr.enOpType=OP_TYPE_AUTO;
    HI_MPI_ISP_SetFPNAttr(0, &pstFPNAttr);

    //========================================================================================================
    //DE 改善噪声形态， 使得噪声均匀细碎， 同时改善边缘毛糙。
    //========================================================================================================
    mppIspDeCtrl(0,DE_enable,DE_model,DE_GlobaGain,DE_GainLF,DE_GainHF) ;

    //========================================================================================================
    //LDCI 是局域自动对比度增强的简称（Local Dynamic Contrast Improvement）
    //========================================================================================================
    mppIspLdciCtrl(0,LDCI_en,LDCI_mode,LDCI_LPFSigma, LDCI_Blc, LDCI_posmean,LDCI_possigma,LDCI_poswgt, LDCI_negmean,LDCI_negsigma,LDCI_negwgt);
}

//=======================================================================================================
//0 bat 1 sd 2 photo 3-4:month 5-6:day
void * osdupdate_Thread(void *pArgs)
{
    while(1)
    {
        //isp_ctrl();
        //mppOsdInfoUpdate();
        usleep(500000);
    }
}

//=======================================================================================================
void * photo_tsk_delay(void *para)
{
    int data =0;
    int id = * (int *)para;
    while(1)
    {
        OSA_MSG_Get(id);
        printf("Take Image,%d\n",data);
        usleep(1100000);                 //200000
        photo = 0;
    }
}

//=======================================================================================================
// 摄像头初始化
//=======================================================================================================
void camera_init()
{
    int sensor_dev;
    pthread_t tid1;
    queue_init(&key_queue);
    queue_init(&key_queue_uart); 
    sensor_dev = read_osd_parameter_key("Option","connect_device",1);           //判断是什么摄像头

    if(sensor_dev==1)
        global_osd_info.scale_mode = read_osd_parameter_key("Option","Size",2);
    else if(sensor_dev==0)
        global_osd_info.scale_mode = read_osd_parameter_key("Option","Size",3);

    printf("\r\n");
    printf("INFO:: Camera_init. VI:%d VO_scale_size:%d\r\n",sensor_dev,global_osd_info.scale_mode);
    switch(sensor_dev)
    {
        case 1: sensorId = 1;  break;    //OV426
        case 2: sensorId = 0;  break;    //OS01A
        case 3: sensorId = 1;  break;    //UVC
        default:sensorId = 1;  break;    //RESERVE
    }

    configInit(sensorId,global_osd_info.scale_mode);    //初始化屏幕和摄像头(VI&VO)
    sysInit();
    SAMPLE_COMM_VI_StartVi(&stViConfig);
    //viVpssInit(stViConfig,SCREEN_WIDTH,SCREEN_HEIGHT);
    viVpssInit(stViConfig,1920,1080);
    if(sensorId==1)
        mppisp_ov426_ctrl();
    else if(sensorId==0)
        mppisp_os01a_ctrl();

    //1.todo add venc 
    //2.and transfor stream to rtsp


    SAMPLE_COMM_VO_StartVO(&stVoConfig);
    mppOsdCreate2(PHOTO_X,PHOTO_Y,PHOTO_WIDTH,PHOTO_HEIGHT,MinHadle[0],PATH_PHOTOS_10INCH,&astVgsAddOsd[0]);
    VSB_osd_init();
    mppOsdInfoUpdate();                                 //手动更新OSD，第一次启动会初始化
    mppClean(1,fbp,1);                                  //mode0=toolbar, mode1=full, mode2=play  mode3=no video, mode4=hdmi,
    pthread_create(&tid1, 0, osdupdate_Thread, NULL);   //OSD刷新线程

    OSA_MSG_Init_One(OSA_CAMERA_PHOTO,photo_tsk_delay);
    if(sensor_dev==1)
        system("sh /usr/qt5.12/demos/sensor_rst.sh > /dev/null");
}

//=======================================================================================================
//拍照函数
//=======================================================================================================
void camera_save_photo_slot(char * path)
{
    HI_U32             u32Profile = 0;
    HI_BOOL            bRcnRefShareBuf = HI_TRUE;
    VENC_GOP_ATTR_S    stGopAttr;
    SIZE_S stVencSize;
    if(frame == 0)
    {
        stVencSize.u32Width   = 800;                //displaywidth;
        stVencSize.u32Height  = 800;                //displayheight; 目前尺寸是800X800，有需要的时候可以修改
        mppVencStart(PT_JPEG,stVencSize,30,VpssChn,0,u32Profile,bRcnRefShareBuf,&stGopAttr,path);
        printf("Save_image::%s\n",path);
        mppVencStop(VpssChn);
        photo = 1;                                  //显示拍照完成标志位置于1
        OSA_MSG_Put(OSA_CAMERA_PHOTO,0);
    }
}

//=======================================================================================================
//录像函数
//=======================================================================================================
void camera_save_video_slot(char * path)
{
    HI_U32              u32Profile = 0;
    HI_BOOL             bRcnRefShareBuf = HI_TRUE;
    VENC_GOP_ATTR_S     stGopAttr;
    SIZE_S              stVencSize;
    static char save_video = 0;
    stVencSize.u32Width  = displaywidth;
    stVencSize.u32Height = displayheight;
    save_video = !save_video;
    printf("Record State::%d\n",save_video);
    if( (frame == 0) || (record == 1) )
    {
        if(save_video)
        {
            record = 1;
            printf("Save_video::%s\n",path);
            stGopAttr.enGopMode  = VENC_GOPMODE_NORMALP;
            stGopAttr.stNormalP.s32IPQpDelta = 2;
            mppVencStart(PT_H265,stVencSize,30,VpssChn,0,u32Profile,bRcnRefShareBuf,&stGopAttr,path);
        }
        else
        {
            record = 0;
            mppVencStop(VpssChn);
        }
    }
}

//=======================================================================================================
//HDMI插入时转换显示的函数
//=======================================================================================================
void camera_displayChange(int hdmi)
{

    static int full_tmp = 1;
    configInit(sensorId,!hdmi);                                                     //初始化HDMI设备
    viVpssDeinit();                                                                 //将现有的Vi和VPSS销毁
    mppDisplayResize(stViConfig,stVoConfig,displaywidth,displayheight);             //VIVPSS初始化和重启VO

    //full 0=toolbar, 1=full, 2=play  3=no video, 4=hdmi,
    if(hdmi)                // HDMI插入检测到信号hdmi=1时,mppClean刷新模式为4
    {
        full_tmp = full;    //int full = 1;
        full = 4;           //fbp刷新模式调整为hdmi模式
    }
    else                    //mppClean刷新模式为1
        full = full_tmp;
}

//=======================================================================================================
//画面冻结函数
//=======================================================================================================
void camera_video_freezz_frame(int state)
{
    if(state)
        video_freeze_state = 1;     //改变冻结标志位
    else
        video_freeze_state = 0;
}

//=======================================================================================================
//
//=======================================================================================================
void camera_showpicfSet_status(char i)
{
    is_show = i;
}

//=======================================================================================================
//
//=======================================================================================================
void camera_deinit(void)
{
    SAMPLE_COMM_VO_StopVO(&stVoConfig);
    SAMPLE_COMM_VI_StopVi(&stViConfig);
}


//=======================================================================================================
//
//=======================================================================================================
pthread_mutex_t osd_mutex = PTHREAD_MUTEX_INITIALIZER;  //PTHREAD_MUTEX_INITIALIZER来静态的初始化锁
void camera_clean(int mode)
{
    pthread_mutex_lock(&osd_mutex);
    mppClean(mode,fbp,0);
    pthread_mutex_unlock(&osd_mutex);
}


//=======================================================================================================
//摄像头主线程
//=======================================================================================================
void * camera_run(void *pArgs)
{
    printf("INFO::(0x%lx): camera_run\n",pthread_self());               //打印线程
    
    int showFlag = 1;
    int frame_prv = 0;
    char mesg[100];
    int vdencFlag = 0;
    VDEC_THREAD_PARAM_S     stVdecSend[VDEC_MAX_CHN_NUM];
    pthread_t               VdecThread[2*VDEC_MAX_CHN_NUM];
    VIDEO_FRAME_INFO_S      stFrameInfom;
    system("i2c_write 0 0xc8 0x06 0x81 1 1 >/dev/null");
    system("i2c_write 0 0xc8 0x01 0x32 1 1 >/dev/null");
    printf("INFO::camera_thread camera_running !!\n");

//=================dzq=========================
    SIZE_S stSize = {800,800};
    HI_U32 u32FrameRate = 25;
    VENC_CHN VencChn = 0;
    HI_U32 u32Profile = 0;
    HI_BOOL bRcnRefShareBuf = HI_TRUE;
    VENC_GOP_ATTR_S pstGopAttr;
    pstGopAttr.enGopMode = VENC_GOPMODE_NORMALP;
    pstGopAttr.stNormalP.s32IPQpDelta = 2;
    char *path = "rtsp.265"; // no use
    static HI_U32 start_venc = 0;
//=================dzq [end]===================
    while(1)
    {
        if(is_show)
        {
            if(showFlag == 0)
            {
                showFlag = 1;
                viVpssInit(stViConfig,displaywidth,displayheight);      //没有显示的时候先初始化vivpss
                
            }
            frame =  mppFrameGet(VpssChn,0,&stFrameInfom);              //从vpss里获取图像

            if(frame != 0)                                              //用于判断OSD是否显示无信号"NO SIGNAL"标识
                nosignal = 1;
            else
                nosignal = 0;

            if((frame_prv !=0)&&(frame==0))                             //没有获取到图像时初始化ov426确保接上摄像头时就能显示图像
                system("sh /usr/qt5.12/demos/sensor_rst.sh >/dev/null");
            frame_prv = frame;
            if(start_venc == 0)
            {
                printf("============================================>>[%d]\r\n",__LINE__);
                SAMPLE_COMM_VENC_Start(PT_H265,stSize, u32FrameRate,VpssChn, VencChn, u32Profile, bRcnRefShareBuf, &pstGopAttr, path);
                start_venc = 1;
            }
            
            if(is_show)
            {
                camera_clean(4);        //显示fbp   //mode0=toolbar, mode1=full, mode2=play  mode3=no video, mode4=hdmi,
                //mppClean(full,fbp,0); //显示fbp   //mode0=toolbar, mode1=full, mode2=play  mode3=no video, mode4=hdmi,

            }
            if(frame == 0 && !video_freeze_state)
            {
                mppFramePut(VpssChn,0,photo,&stFrameInfom);             //没有冻结的时候正常输出图像
                is_cameraopen = 1;
            }
            else if(frame == 0 && video_freeze_state)
            {
                HI_MPI_VPSS_ReleaseChnFrame(VpssChn,0,&stFrameInfom);   //冻结的时候直接释放通道
            }
        }
        else
        {
            if(showFlag == 1)
            {
                showFlag = 0;
                viVpssDeinit();
                is_cameraopen = 0;
            }
        }

        if(startPlayAvi)//播放视频
        {
            if(vdencFlag == 0)//解码过程
            {
                if(mppVdecDecode(videoPath)<0)
                {
                    startPlayAvi = 0;
                    continue;
                }
                vdencFlag = 1;
                char *ptr[4];
                char *p;
                int length = 0;
                vdencVpssInit(framew,frameh);
                sprintf(mesg,"rm %s -f",h265Path);
                ptr[length] = strtok_r(h265Path,"/",&p);
                while(ptr[length] != NULL)
                {
                    length++;
                    ptr[length] = strtok_r(NULL,"/",&p);
                }
                switch(length)
                {
                    case 2:
                    snprintf(stVdecSend[0].cFilePath, sizeof(stVdecSend[0].cFilePath), "/%s",ptr[0]);
                    snprintf(stVdecSend[0].cFileName, sizeof(stVdecSend[0].cFileName), "%s",ptr[1]);
                    break;
                    case 3:
                    snprintf(stVdecSend[0].cFilePath, sizeof(stVdecSend[0].cFilePath), "/%s/%s",ptr[0],ptr[1]);
                    snprintf(stVdecSend[0].cFileName, sizeof(stVdecSend[0].cFileName), "%s",ptr[2]);
                    break;
                }
                //snprintf(stVdecSend[0].cFileName, sizeof(stVdecSend[0].cFileName), "%s",strtok(videoPath,"video/"));
                //snprintf(stVdecSend[0].cFilePath, sizeof(stVdecSend[0].cFilePath), "/%s",strtok(videoPath,"/"));
                printf("play path:%s,play file %s\n",stVdecSend[0].cFilePath,stVdecSend[0].cFileName);
                stVdecSend[0].enType          = PT_H265;
                stVdecSend[0].s32StreamMode   = VIDEO_MODE_FRAME;
                stVdecSend[0].s32ChnId        = 0;
                stVdecSend[0].s32IntervalTime = 20000;
                stVdecSend[0].u64PtsInit      = 0;
                stVdecSend[0].u64PtsIncrease  = 0;
                stVdecSend[0].eThreadCtrl     = THREAD_CTRL_START;
                stVdecSend[0].bCircleSend     = HI_FALSE;
                stVdecSend[0].s32MilliSec     = 0;
                stVdecSend[0].s32MinBufSize   = (1920 * 1080 * 3)>>1;
                SAMPLE_COMM_VDEC_StartSendStream(1, &stVdecSend[0], &VdecThread[0]);//开始播放
                camera_clean(2);
                //mppClean(2,fbp,1);  //mode0=toolbar, mode1=full, mode2=play  mode3=no video, mode4=hdmi,[mode=2 视频解码过程时不显示摄像头图像]
            }
            else
            {
                if(pausePlayAvi)
                {
                    stVdecSend[0].eThreadCtrl = THREAD_CTRL_PAUSE;//设置进程暂停
                }
                else
                {
                    stVdecSend[0].eThreadCtrl = THREAD_CTRL_START;//视频重播
                    camera_clean(2);
                    //mppClean(2,fbp,0);//mode0=toolbar, mode1=full, mode2=play  mode3=no video, mode4=hdmi,[mode=2 播放视频时不显示摄像头图像]
                }

            }
        }
        else  //没有播放视频
        {
            playFlag = 0;
            if(vdencFlag == 1)
            {
                vdencFlag = 0;
                SAMPLE_COMM_VDEC_StopSendStream(1, &stVdecSend[0], &VdecThread[0]);
                vdencVpssDeinit();
                system(mesg);
            }
        }
	}
}
